<?php
class TaskError
{

    /**
     * @var string 错误日志路径
     */
    protected $logFile;

    public function __construct($logFile)
    {
        $this->logFile = $logFile;
    }

    /**当前时间
     * @return false|string
     */
    public function now()
    {
        return date('Y-m-d H:i:s');
    }

    /**写日志
     * @param $message
     */
    public function log($message)
    {
        if (!empty($str = $this->getMessage($message))) {
            file_put_contents($this->logFile, $this->now().'：'.$str.PHP_EOL);
        }
    }

    /**获取错误信息
     * @param string $message
     * @return string
     */
    public function getMessage($message) {
        if ($message instanceof \Exception) {
            return $message->getMessage().$message->getLine().PHP_EOL.$message->getTraceAsString();
        } elseif (is_string($message)) {
            return $message;
        } elseif (is_object($message) && method_exists($message, '__toString')) {
            return (string)$message;
        }
        return '';
    }
}
